package bmark

import grails.test.*
import grails.converters.*

class BookmarkControllerTests extends ControllerUnitTestCase {
    protected void setUp() {
        super.setUp()
        mockDomain(Bookmark)
        controller.bookmarkService = new BookmarkService()
    }

    protected void tearDown() {
        super.tearDown()
    }

    void testIndex() {
        controller.index()
        assertEquals "/timeline", controller.redirectArgs["uri"]
    }

    void testSuccessfulSaveShouldReturn201AndRenderSavedInstance() {
        controller.params.url = "http://www.google.com"

        controller.save()
        assertEquals 201, controller.response.status
        def json = controller.response.contentAsString
        def bmark = JSON.parse(json)
        assertEquals controller.params.url, bmark.url
    }

    void testCompleteUrlWithProtocol() {
        controller.params.url = "www.google.com"

        controller.save()
        assertEquals 201, controller.response.status
        def json =  controller.response.contentAsString
        def bmark = JSON.parse(json)
        assertEquals 'http://' + controller.params.url, bmark.url
    }
    
    
    void testFailingCompleteUrlWithProtocol() {
        controller.params.url = "www.foo.bar"

        controller.save()
        assertEquals 400, controller.response.status
    }

    void testFailingSaveShouldReturn400() {
        controller.params.url = "http://www.foo.bar"

        controller.save()
        assertEquals 400, controller.response.status
    }
    
    void testSuccessfulDeleteShouldReturn200() {
        def bookmark = new Bookmark(url:"http://www.google.com").save()
        controller.params.id = bookmark.id

        controller.delete()
        assertEquals 200, controller.response.status
        assertEquals 'Ok', controller.response.contentAsString
    }

    void testDeleteShouldReturn404OnObjectNotFound() {
        controller.params.id = 42

        controller.delete()
        assertEquals 404, controller.response.status
        assertEquals 'Not found', controller.response.contentAsString
    }
}
